#!/bin/bash

echo "Installation of ShapeAnalysis dependencies"
echo "This script written by Guangfu Wang at Beijing"

function perform_source_change(){
    sudo cp /etc/apt/sources.list /etc/apt/sources.list_backup

    source_str = "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse\n
                deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse\n
                deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse\n
                deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse\n
                deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse\n"
    sudo echo -e source_str > /etc/apt/sources.list
    sudo apt-get update
    sudo apt-get upgrade
    return 0
}

function perform_source_back(){
    sudo rm -f /etc/apt/sources.list
    sudo cp /etc/apt/sources.list_backup /etc/apt/sources.list
    sudo apt-get update
    sudo apt-get upgrade
    echo "Source center has been restored"
    return 0
}

function install_packages(){
    sudo apt-get install build-essential
    sudo apt-get install libeigen3-dev
    sudo apt-get install libglm-dev
    sudo apt-get install libglew-dev
    sudo apt-get install qt5-default
    sudo apt-get install libassimp-dev
    sudo apt-get install doxygen
    return 0
}

echo "Change source for downloading"

echo -n "Confirm change of source?[Y/N]:"
read confirm_change_source
##change it to lowercase
echo $confirm_change_source | tr 'A-Z' 'a-z'

if [ -z $confirm_change_source ];then
   echo "Please input y/n"
   exit
else
    if [ $confirm_change_source = "y" -o $confirm_change_source = "yes" ]; then
        echo "Performing changing source"
        perform_source_change()
        echo "Installing packages..."
        install_packages()
        echo "Restoring source..."
        perform_source_back()
        exit
    elif [ $confirm_change_source = "n" -o $confirm_change_source = "no" ]; then
        echo "No change of source"
        echo "Installing packages..."
        install_packages()
        exit
    else
        Error "You input unknown info..."
        exit
    fi
fi
